﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Message
    {
        public static Message FindByMessageID_IncludeDeleted(linqDatabaseDataContext db, int MessageID)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.MessageID == MessageID select u).SingleOrDefault());
        }

        public static Message FindByMessageID(linqDatabaseDataContext db, int MessageID)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.MessageID == MessageID select u).SingleOrDefault());
        }

        public static List<Message> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<Message> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<Message> ToListByRequestID_IncludeDeleted(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sMessages where v.RequestID == RequestID select v.ID).Contains(u.MessageID) select u).ToList());
        }

        public static List<Message> ToListByRequestID(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Messages where u.ID == (from v in db.Messages where v.MessageID == u.MessageID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sMessages where v.RequestID == RequestID select v.ID).Contains(u.MessageID) && !u.IsDeleted select u).ToList());
        }

        public static List<Message> HistoryToListByMessageID(linqDatabaseDataContext db, int MessageID)
        {
            return (from u in db.Messages where u.MessageID == MessageID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
